#include "QPositionCalculator.h"

int QPositionCalculator::FormBorderWidth = 3;
int QPositionCalculator::FormTitleBarHeight = 30;

QPositionCalculator::QPositionCalculator(QObject *parent) : QObject(parent)
{
    Reset();
    installEventFilter(parent);
    ParentForm = qobject_cast<QWidget*>(parent);
    FormRubberBand= NULL;
    FormFlags = ParentForm->windowFlags();
    ParentForm->setMouseTracking(true);
    ParentForm->setAttribute(Qt::WA_Hover, true);
    UpdateRubberBandStatus();
}

QPositionCalculator::~QPositionCalculator()
{

}

void QPositionCalculator::Reset()
{
    OnEdges = false;
    OnLeftEdge = false;
    OnRightEdge = false;
    OnTopEdge = false;
    OnBottomEdge = false;
    OnTopLeftEdge = false;
    OnBottomLeftEdge = false;
    OnTopRightEdge  = false;
    OnBottomRightEdge = false;

    FormIsMovable        = true;
    FormIsResizable      = true;
    FormRubberBandIsOnResize = true;
    FormRubberBandIsOnMove   = true;

    CursorShapeIsChanged = false;
}

void QPositionCalculator::Calculate(const QPoint &globalMousePosition, const QRect &frameRect)
{
    int xMousePostion = globalMousePosition.x();
    int yMousePostion = globalMousePosition.y();

    int FrameLeftEdgePosition = frameRect.x();
    int FrameTopEdgePosition = frameRect.y();
    int FrameRightEdgePosition = FrameLeftEdgePosition + frameRect.width();
    int FrameBottonEdgePosition = FrameTopEdgePosition + frameRect.height();

    /** 四条边上 */
    OnLeftEdge = (xMousePostion >= FrameLeftEdgePosition &&
                  xMousePostion <= (FrameLeftEdgePosition + FormBorderWidth));
    OnRightEdge = (xMousePostion <= FrameRightEdgePosition &&
                   xMousePostion >= FrameRightEdgePosition - FormBorderWidth );
    OnTopEdge = (yMousePostion >= FrameTopEdgePosition &&
                 yMousePostion <= (FrameTopEdgePosition + FormBorderWidth));
    OnBottomEdge = (yMousePostion <= FrameBottonEdgePosition &&
                    yMousePostion >= (FrameBottonEdgePosition - FormBorderWidth));

    /** 四个角上 */
    OnTopLeftEdge = OnTopEdge && OnLeftEdge;
    OnBottomLeftEdge = OnBottomEdge && OnLeftEdge;
    OnTopRightEdge = OnTopEdge && OnRightEdge;
    OnBottomRightEdge = OnBottomEdge && OnRightEdge;

    /** 任意一条边上 */
    OnEdges = OnLeftEdge || OnRightEdge || OnTopEdge || OnBottomEdge;
    if(OnEdges) {
        //qDebug() << "OnEdges";
    }
}

void QPositionCalculator::SetFormMovable(bool movable)
{
    FormIsMovable = movable;
}

void QPositionCalculator::SetFormResizable(bool resizable)
{
    FormIsResizable = resizable;
}

void QPositionCalculator::SetFormRubberBandOnMove(bool movable)
{
    FormRubberBandIsOnMove = movable;
}

void QPositionCalculator::SetFormRubberBandOnResize(bool resizable)
{
    FormRubberBandIsOnResize = resizable;
}

void QPositionCalculator::SetFormBorderWidth(uint width)
{
    QPositionCalculator::FormBorderWidth = width;
}

void QPositionCalculator::SetFormTitleBarHeight(uint height)
{
    QPositionCalculator::FormTitleBarHeight = height;
}

bool QPositionCalculator::IsFormResizable()
{
    return FormIsResizable;
}

bool QPositionCalculator::ISFormMovable()
{
    return FormIsMovable;
}

bool QPositionCalculator::IsFormRubberBandOnMove()
{
    return FormRubberBandIsOnMove;
}

bool QPositionCalculator::IsFormRubberBandOnResisze()
{
    return FormRubberBandIsOnResize;
}

unsigned long QPositionCalculator::GetFormBorderWidth()
{
    return QPositionCalculator::FormBorderWidth;
}

unsigned long QPositionCalculator::GetFormTitleBarHeight()
{
    return QPositionCalculator::FormTitleBarHeight;
}

void QPositionCalculator::mouseMoveEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton) {

    }
}

void QPositionCalculator::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton) {

    }
}

void QPositionCalculator::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton) {

    }
}

void QPositionCalculator::UpdateCursorShape(const QPoint &globalMousePosition)
{
    if (ParentForm->isFullScreen() || ParentForm->isMaximized())
    {
        if (CursorShapeIsChanged)
        {
            ParentForm->unsetCursor();
        }
        return;
    }

    Calculate(globalMousePosition, ParentForm->frameGeometry());

    if(OnTopLeftEdge || OnBottomRightEdge)
    {
        ParentForm->setCursor( Qt::SizeFDiagCursor );
        CursorShapeIsChanged = true;
    }
    else if(OnTopRightEdge || OnBottomLeftEdge)
    {
        ParentForm->setCursor( Qt::SizeBDiagCursor );
        CursorShapeIsChanged = true;
    }
    else if(OnLeftEdge || OnRightEdge)
    {
        ParentForm->setCursor( Qt::SizeHorCursor );
        CursorShapeIsChanged = true;
    }
    else if(OnTopEdge || OnBottomEdge)
    {
        ParentForm->setCursor( Qt::SizeVerCursor );
        CursorShapeIsChanged = true;
    }
    else
    {
        if (CursorShapeIsChanged)
        {
            ParentForm->unsetCursor();
            CursorShapeIsChanged = false;
        }
    }
}

void QPositionCalculator::ResizeForm(const QPoint &globalMousePosition)
{

}

void QPositionCalculator::UpdateRubberBandStatus()
{
    if (FormRubberBandIsOnMove || FormRubberBandIsOnResize) {
        if (NULL == FormRubberBand) {
            FormRubberBand = new QRubberBand(QRubberBand::Rectangle);
        }
    } else {
        delete FormRubberBand;
        FormRubberBand = NULL;
    }
}
